// import request from "../api"
import { AxiosPromise } from "axios"
import { LoginData, LoginResult, VerifyCodeResult } from "./type"
import request from "../http"
// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID

/**
 * @param data {LoginData}
 * @returns
 */
export function login(data: LoginData): AxiosPromise<LoginResult> {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || "password"
  }
  return request({
    url: "/auth/login",
    headers: {
      isToken: false,
      isEncrypt: true
    },
    method: "post",
    data: params
  })
}

export function updateUserInformation(data: any) {
  return request({
    url: "user/updateUser",
    method: "post",
    data
  })
}

export function checkLogin() {
  return request({
    url: "user/checkLogin",
    method: "get"
  })
}

export function loginOut() {
  return request({
    url: "/auth/logout",
    method: "post"
  })
}

export function register(data: any) {
  return request({
    url: "user/register",
    method: "post",
    data
  })
}


export function destroyAccount() {
  return request({
    url: "user/destroy",
    method: "post"
  })
}

export function getCode(data: any) {
  return request({
    url: "user/getCode",
    method: "post",
    data
  })
}

export function loginWithCode(data: any) {
  return request({
    url: "user/loginWithCode",
    method: "post",
    data
  })
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return request({
    url: "/auth/code",
    headers: {
      isToken: false
    },
    method: "get",
    timeout: 20000
  })
}
